<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sa="http://www.w3.org/1999/xhtml" lang="">
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <link th:href="@{/pear/component/pear/css/pear.css}" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" method="POST" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">部门名称</label>
                    <div class="layui-input-inline">
                        <input id="departmentName" type="text" name="departmentName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-md" lay-submit lay-filter="department-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="department-table" lay-filter="department-table"></table>
    </div>
</div>
<script type="text/html" id="department-toolbar">
    <button class="layui-btn  layui-btn-sm" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>
<script type="text/html" id="department-bar">
    <button class="layui-btn  layui-btn-sm" lay-event="edit"><i
            class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i
            class="layui-icon layui-icon-delete"></i>
    </button>
</script>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">
    layui.use(['table', 'form', 'jquery', 'treetable'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let treetable = layui.treetable;
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let cols = [
            [
                {type: 'checkbox'},
                {title: '部门名', field: 'departmentName', align: 'left'},
                {title: '描述', field: 'description', align: 'left'},
                {title: '操作', toolbar: '#department-bar', align: 'center'}
            ]
        ]
        window.render = function () {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'departmentId',
                treePidName: 'parentId',
                skin: 'line',
                treeDefaultClose: true,
                toolbar: '#department-toolbar',
                elem: '#department-table',
                url: baseURL + 'department/list',
                page: true,
                cols: cols
            });
        }

        render();
        table.on('tool(department-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(department-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });
        form.on('submit(department-query)', function (data) {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'departmentId',
                treePidName: 'parentId',
                skin: 'line',
                treeDefaultClose: true,
                toolbar: '#department-toolbar',
                elem: '#department-table',
                url: baseURL + 'department/getParent',
                where: {
                    departmentName: data.field.departmentName
                },
                parseData: function (res) {
                    return res;
                },
                response: {
                    statusCode: 1000 //规定成功的状态码，默认：0
                },
                type: 'get',
                page: false,
                cols: cols
            });
            return false;
        });
        document.getElementById('resetBtn').addEventListener('click', function () {
            // 重新加载表格，不带任何过滤条件
            table.reload('department-table', {
                where: {}, // 清除过滤条件
            });
        });
        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                move: true,
                area: ['800px', '600px'],
                content: 'department-add'
            });
        }
        window.edit = function (obj) {
            window.departmentId = obj.data.departmentId;
            $.ajax({
                url: baseURL + "department/getDepartmentByDepartmentId/" + obj.data.departmentId,
                type: 'get',
                success: function (res) {
                    window.parentId = res.data.parentId
                }
            })
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['800px', '600px'],
                content: 'department-edit',
                success: function (layero, index) {
                    let body = layer.getChildFrame('body', index);
                    let data = obj.data;
                    body.find('input[name=departmentName]').val(data.departmentName)
                    body.find('input[name=description]').val(data.description)
                }
            });
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除该部门吗', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: baseURL + 'department/delete/' + obj.data['departmentId'],
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        table.reload('department-table', {where: {}, page: {curr: 1}});
                        if (result.code === 1000) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 2000
                            }, function () {

                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }
                })


            });
        }
        window.refresh = function () {
            table.reload('department-table');
        }
    })
</script>
</body>
</html>
